package com.example.newfragemnt;

import android.content.Context;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {
    private TextView txt_title;//新闻TextView新闻标题
    private FrameLayout fl_content;//新闻内容容器
    private Context mContext;//当前Activity的Context
    private ArrayList<Data> datas=null;//data数据的集合
    private FragmentManager fManager=null;//fragment的管理器
    private long endTime=0;
    private android.app.FragmentTransaction ft;
    private RadioGroup rg;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        rg= (RadioGroup) findViewById(R.id.rg);
        //得到fragment的管理器
        fManager=getSupportFragmentManager();

        //new 出集合
        datas=new ArrayList<Data>();
        //循环的集合
        for (int i=0;i<10;i++){
            Data d=new Data("新闻标题"+i,"新闻内容"+i);
            //注意要添加到集合当中
            datas.add(d);
        }
        //开启事物
        FragmentTransaction ft=fManager.beginTransaction();

        //得到我们的fragmentfl_content
        NewListFragment nf=new NewListFragment(fManager,datas);
        //得到fragment的容器
        fl_content= (FrameLayout) findViewById(R.id.fl_content);
        //用得到我们的fragment去替换我们fl_content
        ft.replace(R.id.fl_content,nf);
        //事物进行提交

        ft.commit();

    }
    public void c(View view){

        int id=view.getId();

        android.app.FragmentManager fm=getFragmentManager();
        switch (id){

            case R.id.bt2:
                ;
               wx1 wx1=new wx1();
                android.app.FragmentTransaction ft1=fm.beginTransaction();
                ft1.replace(R.id.fl_content,wx1);
                ft1.commit();
                break;
            case R.id.bt3:

                wx2 wx2=new wx2();
                android.app.FragmentTransaction ft2=fm.beginTransaction();
                ft2.replace(R.id.fl_content,wx2);
                ft2.commit();
                break;
            case R.id.bt4:
                wx3 wx3=new wx3();
                android.app.FragmentTransaction ft3=fm.beginTransaction();
                ft3.replace(R.id.fl_content,wx3);
                ft3.commit();
                break;
        }
    }

    @Override
    public  void  onBackPressed(){
        //表示我们的fragment的栈中没有任何一个fragment，那么这个程序就可以退了
        if (fManager.getBackStackEntryCount()==0){
            if ((System.currentTimeMillis()-endTime)>2000){
                Toast.makeText(this, "再按一次退出程序", Toast.LENGTH_SHORT).show();
                endTime = System.currentTimeMillis();
            }else {
                //直接退出
                super.onBackPressed();
            }
        }else {
            //后面的NewListFragment推出显示，事实上就是把当前NewContentFragment挤出回栈最后
            fManager.popBackStack();

        }

    }
}

